package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.util.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 错题解析的完形填空
 */
public class WanXingThreeAdapter extends BaseAdapter {
    List<TIbean.Clild> list;
    Context context;
    int position;
    boolean isShowAnswerTrue;
    String content;

    public WanXingThreeAdapter(Context context, List<TIbean.Clild> list) {
        this.context = context;
        this.list = list;
    }
    public WanXingThreeAdapter(Context context,List<TIbean.Clild> list,int position,boolean isShowAnswerTrue){
        this.context = context;
        this.list = list;
        this.isShowAnswerTrue=isShowAnswerTrue;
        this.position=position;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolders holder = null;
        if (view == null) {
            holder = new ViewHolders();
            view = LayoutInflater.from(context).inflate(R.layout.item_wanxing_two, null, false);
            holder.layout = (FlowLayout) view.findViewById(R.id.layout);
            holder.text = (TextView) view.findViewById(R.id.text);
            view.setTag(holder);
        } else {
            holder = (ViewHolders) view.getTag();
        }
        holder.layout.removeAllViews();
        holder.text.setText(position + 1 + "");
        for (int j = 0; j < list.get(position).getOption().size(); j++) {
            answerLineContentAdd(holder.layout, list.get(position).getOption(),list.get(position).getAnwser_true(), list.get(position).getAnswers().get(j), j, position);
        }

        return view;
    }

    /**
     * 答案
     */
    private void answerLineContentAdd(final FlowLayout answerLine, final ArrayList<TIbean.Options> qianzhui,ArrayList<String> answers, String content, final int j, final int position) {
        // 设置添加的textvIew的参数
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        params.setMargins(0, 5, 25, 5);

        final View innerView = LayoutInflater.from(MyApplication.instance).inflate(R.layout.item_a3, null, false);
        final TextView a3_title = (TextView) innerView.findViewById(R.id.a3_title);
        final TextView a3_content = (TextView) innerView.findViewById(R.id.a3_content);
        final ImageView a3_img = (ImageView) innerView.findViewById(R.id.a3_img);
        final RelativeLayout a3_layout = (RelativeLayout) innerView.findViewById(R.id.a3_layout);
        a3_title.setText(qianzhui.get(j).getOptions());
        a3_content.setText(content);
        answerLine.addView(innerView, params);


        if(isShowAnswerTrue){
            if (qianzhui.get(j).getOptions().equals(answers.get(0))) {
                a3_layout.setBackgroundResource(R.drawable.shape_a33);
                a3_title.setBackgroundResource(R.color.olive_green);
                a3_title.setTextColor(context.getResources().getColor(R.color.white));
                a3_img.setVisibility(View.VISIBLE);
            } else {
                a3_layout.setBackgroundResource(R.drawable.shape_a3);
                a3_title.setBackgroundResource(R.color.gray_D6);
                a3_title.setTextColor(context.getResources().getColor(R.color.blue));
                a3_img.setVisibility(View.GONE);
            }
        }



    }

    class ViewHolders {
        FlowLayout layout;
        TextView text;
        TextView content;
    }
}
